package com.hiwan.unions.activity.main.homeInform.informFragment;

import android.view.View;
import android.widget.ListView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;
import com.hiwan.unions.adapter.bean.HomeBottonBean;
import com.hiwan.unions.adapter.fragmentAdapters.HomeAdapter;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.JsonUtil;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/6/7.
 */
public class BFragment extends RootFragment implements HttpListener<JSONObject> {
    private ListView myListView;
    HomeAdapter adapter;
    private ArrayList<HomeBottonBean> cDates = new ArrayList<HomeBottonBean>();
    private JSONObject jsonObject;
    private int pageNumber = 1, pageSize = 10;

    @Override
    public int getLayout() {
        return R.layout.fragment_home_list;
    }

    @Override
    protected void Interface() {
        super.Interface();
        myListView = findView(R.id.myListview);
        myListView.setVisibility(View.GONE);

        adapter = new HomeAdapter(mActivity, cDates);
        myListView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        pageNumber = 1;
        showLoading();
        getDate();
    }

    @Override
    public void DataRefresh() {
        super.DataRefresh();
        pageNumber = 1;
        getDate();
    }

    @Override
    public void DataRoadMore() {
        super.DataRoadMore();
        pageNumber++;
        getDate();
    }

    private void getDate() {
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.article_list, RequestMethod.POST);
        request.add("is_city", 0);
        request.add("folder", "province_article");
        request.add("pageNumber", pageNumber);
        request.add("pageSize", pageSize);
        CallServer.getRequestInstance().add(getActivity(), 0, request, this, false);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "list");
        if (pageNumber == 1) {
            cDates.clear();
            adapter.notifyDataSetChanged();
        }
        finishRefresh();
        finishLoadMore(JsonUtil.getBoolean(jsonObject, "lastPage"));
        if (jsonArray.length() == 0 && pageNumber == 1) {
            showeMPTY("暂无列表信息");
        } else {
            showContentView();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                HomeBottonBean homeBottonBean = new HomeBottonBean();
                homeBottonBean.setDate(JsonUtil.getString(jsonObject, "publish_time"));
                homeBottonBean.setImage_url(JsonUtil.getString(jsonObject, "image_url"));
                homeBottonBean.setImage_net_url(JsonUtil.getString(jsonObject, "image_net_url"));
                homeBottonBean.setTitle(JsonUtil.getString(jsonObject, "title"));
                homeBottonBean.setUuid(JsonUtil.getString(jsonObject, "uuid"));
                homeBottonBean.setJump_url(JsonUtil.getString(jsonObject, "jump_url"));
                cDates.add(homeBottonBean);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        if (type) {
            showError();
        }
    }
}